package edu.nf.ch01.service;

import edu.nf.ch01.entity.User;
import edu.nf.ch01.mapper.UserMapper;
import edu.nf.ch01.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Map;

/**
 * @author wangl
 * @date 2025/4/22
 */
public class UserService {

    public void saveUser(User user) {
        SqlSession session = MybatisUtils.getSqlSession();
        try  {
            //创建UserMapper接口的代理对象
            //这个代理对象会解析并执行mapper映射配置文件中的sql
            UserMapper proxy = session.getMapper(UserMapper.class);
            //执行代理对象的save方法
            proxy.save(user);
            //手动提交事务
            session.commit();
        } catch (RuntimeException e) {
            //事务回滚
            session.rollback();
        } finally {
            session.close();
        }
    }


    public void saveUser2(Map<String, Object> user) {
        SqlSession session = MybatisUtils.getSqlSession();
        try  {
            //创建UserMapper接口的代理对象
            //这个代理对象会解析并执行mapper映射配置文件中的sql
            UserMapper proxy = session.getMapper(UserMapper.class);
            //执行代理对象的save方法
            proxy.save2(user);
            //手动提交事务
            session.commit();
        } catch (RuntimeException e) {
            //事务回滚
            session.rollback();
        } finally {
            session.close();
        }
    }
}